package com.controller;

import com.entity.Phone;
import com.entity.User;
import com.packdate.PackCarDate;
import com.resp.BaseResponseEntity;
import com.service.ICartService;
import com.service.IPhoneService;
import com.service.IUserService;
import com.service.impl.CartServiceimpl;
import com.service.impl.PhoneServiceimpl;
import com.service.impl.UserServiceimpl;
import com.utlils.ResponseUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 查询用户的购物车总订单
 */

@WebServlet("/GetCart")
public class GetCartDateController extends HttpServlet {
    IPhoneService phoneService=new PhoneServiceimpl();
    ICartService cartService=new CartServiceimpl();
    IUserService userService=new UserServiceimpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        //查询所有购物车信息
      //  String action = req.getParameter("action");
      //  String action = req.getParameter("action");
//
//        if(action.equals("picture")){
//            PictureCart(req,resp);
//        }else {
            FindAllCart(req,resp);




    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }


    /**
     * 查询所有购物车信息
     * @param req
     * @param resp
     */
    public void FindAllCart(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //根据用户名获取用户ID
        String name = req.getParameter("uname");
        //跟据用户ID获取购物车内所有产品
        List<Phone> all = cartService.findByNameCart(name);
        int sum=0;
        for (Phone p:all) {
            sum+=p.getPrice();
        }
        System.out.println(sum);

        BaseResponseEntity entity=null;
        if(all!=null){
            entity=new BaseResponseEntity(sum,"查到购物车",all);
            System.out.println(all);
        }else {
            entity=BaseResponseEntity.nullCart();
        }
        ResponseUtils.responseToJson(resp,entity);
    }

    /**
     * 将所有商品状态值修改为已拍下
     *
     * @param req
     * @param resp
     */
    public void PictureCart(HttpServletRequest req, HttpServletResponse resp){
        String name = req.getParameter("uname");
        List<Phone> all = cartService.findByNameCart(name);

        for (Phone p:all) {
            //将购物车内的状态都修改为3
            Integer integer = cartService.UpDateState(3, p.getId());
        }
        //拍下所有产品

    }


}
